package com.xbell.app.sys.common;

/**
 * @author GXY
 * @description 响应消息
 * @date 2023/06/09
 */


public class ResponseMessage<T> implements java.io.Serializable{
	/**
	 *
	 */
	private static final long serialVersionUID = -8414489110987028889L;

	private int code;
	private String msg;
	private T data;

	/**
	 * @return the code
	 */
	public int getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(int code) {
		this.code = code;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the data
	 */
	public T getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(T data) {
		this.data = data;
	}
	
	public ResponseMessage() {
		
	}
	public ResponseMessage(int code,String msg,T data)
	{
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	public ResponseMessage(ResponseCode code,String msg, T data)
	{
		this.code = code.getCode();
		this.msg = msg;
		this.data = data;
	}

	public String toJSON()
	{
		return com.alibaba.fastjson.JSONObject.toJSONString(this);
		//Gson json = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		//return json.toJson(this);
	}

	/**
	 * 快捷返回200
	 * @deprecated {@link #ResponseMessage.Ok(Object)}
	 * @author 王文胜
	 * @date 2019年8月15日 - 下午2:30:27
	 * @param t
	 * @return
	 */
	@Deprecated
	public static <T> ResponseMessage<T> responseOk(T t)
	{
		return new ResponseMessage<T>(ResponseCode.ResponseCodeOK,"ok",t);
	}
	/**
	 * 快捷返回200
	 * @author 王文胜
	 * @date 2019年8月15日 - 下午2:30:27
	 * @param t
	 * @return
	 */
	public static <T> ResponseMessage<T> ok(T t)
	{
		return new ResponseMessage<T>(ResponseCode.ResponseCodeOK,"ok",t);
	}
	public static <T> ResponseMessage<T> ok()
	{
		return new ResponseMessage<T>(ResponseCode.ResponseCodeOK,"ok",null);
	}
	public static <T> ResponseMessage<T> responseParamsinValid(T t)
	{
		return new ResponseMessage<T>(ResponseCode.ResponseCodeParamNotValidError,"非法参数",t);
	}
	/**
	 * 自定义错误
	 * @author 王文胜
	 * @date 2020年3月4日 - 下午4:06:25
	 * @copyright 合肥讯百软件科技有限公司
	 * @param reson
	 * @return
	 */
	public static <T> ResponseMessage<T> fastResponseMessage(ResponseCode code,String reson,T data)
	{
		return new ResponseMessage<T>(code,reson,data);
	}

	public static <T> ResponseMessage<T> error(String message) {
		return new ResponseMessage<>(ResponseCode.ResponseCodeInnerError, message, null);
	}

	public static <T> ResponseMessage<T> sucOrErr(String msg) {
		if (msg != null) {
			return error(msg);
		}
		return ok();
	}
}
